package myky.com.doctorapp.ui

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.DefaultItemAnimator
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gyf.barlibrary.ImmersionBar
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.android.synthetic.main.activity_choose_disses_type.*
import kotlinx.android.synthetic.main.public_common_title.*
import myky.com.doctorapp.R
import myky.com.doctorapp.adapter.DissesTypeAdapter
import myky.com.doctorapp.base.BaseActivity
import myky.com.doctorapp.contract.ChooseDissesTypeContract
import myky.com.doctorapp.entity.ChooseDisesTypeEntity
import myky.com.doctorapp.entity.PageEntity
import myky.com.doctorapp.presenter.ChooseDissesTypePresenter
import myky.com.doctorapp.utils.Constants


/**
 * Description: 选择疾病类型 和简单的列表选择共用此页面
 * @author xiangyao
 * Created by 2019/2/26 9:43 AM
 */
class ChooseDissesTypeActivity : BaseActivity<ChooseDissesTypePresenter, ChooseDisesTypeEntity>(), ChooseDissesTypeContract.View, OnRefreshLoadMoreListener, BaseQuickAdapter.OnItemChildClickListener {

    var currentPage = 1

    var currentPageSize = 10

    var doctorId = "-1"

    var intentType = -1

    var totealPage = 1

    lateinit var dissesTypeAdapter: DissesTypeAdapter

    lateinit var diseaseList: MutableList<ChooseDisesTypeEntity.DiseaseListBean>


    override fun showDissesType(mDiseaseList: MutableList<ChooseDisesTypeEntity.DiseaseListBean>, pageEntity: PageEntity) {

        totealPage = pageEntity.pages

        diseaseList.addAll(mDiseaseList)


        dissesTypeAdapter.notifyDataSetChanged()


    }

    override fun injectTarget(): View = findViewById(R.id.activity_new_consult_smartRefresh)


    override fun initData() {

        when (intentType) {
            Constants.INTENT_TYPE_NICKNAME -> {

                conmonTitleTextView.text = "疾病类型"

                mPresenter.findDissesType(currentPage.toString(), doctorId, "1", currentPageSize.toString(), Constants.CURRENTVERSION)


            }
            Constants.INTENT_TYPE_VISITS -> {
            }
            Constants.INTENT_TYPE_ISKNOW -> {
            }

        }


    }

    override fun initView() {


        ImmersionBar.with(this)
                .statusBarColor(R.color.colorPrimary)
                .titleBar(R.id.titleLayout)
                .init()

        diseaseList = ArrayList()

        val extras = intent.extras

        intentType = extras.getInt(Constants.INTENT_TYPE)

        doctorId = extras.getString(Constants.DOCTORIDKEY)

        activity_new_consult_smartRefresh.setOnRefreshLoadMoreListener(this)

        dissesTypeAdapter = DissesTypeAdapter(diseaseList)

        activity_new_consult_recycler.layoutManager = LinearLayoutManager(this)

        activity_new_consult_recycler.addItemDecoration(DividerItemDecoration(this, 1))
        activity_new_consult_recycler.itemAnimator = DefaultItemAnimator()
        activity_new_consult_recycler.overScrollMode = View.OVER_SCROLL_NEVER

        activity_new_consult_recycler.adapter = dissesTypeAdapter


        if (intentType == Constants.INTENT_TYPE_NICKNAME) {

            val view = layoutInflater.inflate(R.layout.activity_new_case_type_adapter_header, null)
            val textView = view.findViewById(R.id.activity_new__case_type_adapter_header_tv) as TextView
            textView.text = resources.getString(R.string.new_consult_type_header_text)

            dissesTypeAdapter.addHeaderView(view)
        }


        activity_new_consult_recycler.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {

                    activity_new_consult_smartRefresh.setEnableRefresh(true)

                } else if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
                    activity_new_consult_smartRefresh.setEnableRefresh(false)

                }
            }

        })


        dissesTypeAdapter.onItemChildClickListener = this

    }

    override fun getContenView(): Int = R.layout.activity_choose_disses_type


    override fun showDataSuccess(datas: ChooseDisesTypeEntity?) {
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {

        when (view?.id) {

            R.id.my_list_lin1 -> {

                val data = dissesTypeAdapter.data

                val diseaseListBean = data[position]

                var bundle = Bundle()

                bundle.putString(Constants.DISSESNAME, diseaseListBean.diseasename)
                bundle.putInt(Constants.DISSESID, diseaseListBean.diseaseid)


                var intent = Intent()
                intent.putExtras(bundle)

                setResult(RESULT_OK, intent)

                finish()


            }
        }

    }


    override fun reloadActivity() {
    }

    override fun getPresenter(): ChooseDissesTypePresenter {
        return ChooseDissesTypePresenter()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {

        if (intentType != Constants.INTENT_TYPE_VISITS && intentType != Constants.INTENT_TYPE_ISKNOW) {
            currentPage += 1

            if (currentPage > totealPage) {

                activity_new_consult_smartRefresh.setEnableLoadMore(false)
            } else {

                mPresenter.findDissesType(currentPage.toString(), doctorId, "1", currentPageSize.toString(), Constants.CURRENTVERSION)

            }


        }
        refreshLayout.finishLoadMore(2000)
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {

        if (intentType != Constants.INTENT_TYPE_VISITS && intentType != Constants.INTENT_TYPE_ISKNOW) {
            diseaseList.clear()
            showLoadingView()
            currentPage = 1
            activity_new_consult_smartRefresh.setEnableLoadMore(true)
            mPresenter.findDissesType(currentPage.toString(), doctorId, "1", currentPageSize.toString(), Constants.CURRENTVERSION)

        }
        activity_new_consult_smartRefresh.finishRefresh(2000)
    }
}
